package cn.dankal.flyaudio.store;

import android.view.View;

import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.List;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.CommentsEntity;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.CommentsListAdapter;
import cn.dankal.flyaudio.contract.CommentsListContract;
import cn.dankal.flyaudio.presenter.CommentsListPresenter;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 21/7/2020
 *     desc   : 商品详情评价列表
 *     version: cn.dankal.flyaudio.store
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class CommentsListActivity extends BaseActivity implements  CommentsListContract.View {

    private CommentsListAdapter commentsListAdapter;
    private CommentsListPresenter mPresenter;
    private int position;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_comments_list;
    }

    @Override
    protected void initComponents() {
        commentsListAdapter = new CommentsListAdapter();
        String id = getIntent().getStringExtra("id");
        RecyclerView recyclerView= findViewById(R.id.mRv);
        recyclerView.setAdapter(commentsListAdapter);

        mPresenter = new CommentsListPresenter();
        mPresenter.attachView(this);
        mPresenter.getEvaluate(id,1);

        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        LiveEventBus.get("Like",int.class).observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                position = integer;
                if ("true".equals(commentsListAdapter.getDatas().get(integer).getiLike())) {
                    mPresenter.unLike(commentsListAdapter.getDatas().get(integer).getUuid());
                } else {
                    mPresenter.like(commentsListAdapter.getDatas().get(integer).getUuid());
                }
            }
        });
    }

    @Override
    public void evaluateSuccess(List<CommentsEntity> list) {

        commentsListAdapter.addDatas(list,false);
    }

    @Override
    public void onError() {
    }

    @Override
    public void likeSuccess() {
        commentsListAdapter.getDatas().get(position).setiLike("true");
        commentsListAdapter.notifyDataSetChanged();
    }

    @Override
    public void unLikeSuccess() {
        commentsListAdapter.getDatas().get(position).setiLike("false");
        commentsListAdapter.notifyDataSetChanged();
    }
}
